package cn.newgrand.netcall.integration.message;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.Iterator;

import javax.swing.AbstractAction;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jivesoftware.resource.SparkRes;
import org.jivesoftware.spark.component.VerticalFlowLayout;
import org.jivesoftware.spark.util.GraphicUtils;
import org.jivesoftware.sparkimpl.plugin.alerts.SparkToaster;
import org.jivesoftware.sparkimpl.plugin.scratchpad.Task;

import cn.newgrand.netcall.integration.AppSysMesPlugin;
import cn.newgrand.netcall.integration.AppSysRes;
import cn.newgrand.netcall.integration.IntegrationManager;
import cn.newgrand.netcall.transcript.ChatTranscript;
import cn.newgrand.netcall.transcript.HistoryMessage;
import cn.newgrand.netcall.transcript.HistoryMessageManager;

/**
 * @author SidneyRen E-mail:SidneyRen@hotmail.com
 * @version create time：2010-10-21 上午11:40:54 类说明
 */
public class AppSysToaster extends SparkToaster {

	private AbstractAction showAppSys = new AbstractAction() {

		@Override
		public void actionPerformed(ActionEvent e) {
			HistoryMessageManager.getInstance().showAppHistory(AppSysMesPlugin.ct);
		}
	};

	public AppSysToaster() {
		super();
		setCustomAction(showAppSys);
		setViewLable(false);
	}
	
	public void showToaster(){
		
		ChatTranscript chatTranscript = AppSysMesPlugin.ct;
		Iterator<HistoryMessage> mi = chatTranscript.getMessages().iterator();
		
		final JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP, 0, 0, true, false));
        mainPanel.setBackground(Color.white);
		final JPanel titlePanel = new JPanel(new BorderLayout()) {
            public void paintComponent(Graphics g) {
                Color startColor = Color.white;
                Color endColor = new Color(176, 201, 242);
                Graphics2D g2 = (Graphics2D)g;

                int w = getWidth();
                int h = getHeight();

                GradientPaint gradient = new GradientPaint(0, 0, startColor, w, h, endColor, true);
                g2.setPaint(gradient);
                g2.fillRect(0, 0, w, h);
            }

        };
		
		final JLabel taskLabel = new JLabel("点击查看进入系统");
//        taskLabel.setFont(taskLabel.getFont().deriveFont(Font.BOLD));
        titlePanel.add(taskLabel, BorderLayout.EAST);
        mainPanel.add(titlePanel);
        boolean changeColor = true;
        while (mi.hasNext()) {
			HistoryMessage hs = (HistoryMessage) mi.next();
			String bodyXML = hs.getBody();
			final MessageParser mp = new MessageParser(bodyXML);
			final JPanel item = new JPanel(new BorderLayout());
			item.setOpaque(false);
			
			JLabel contentLabel = new JLabel(" " + mp.getContent() + "("
					+ mp.getChildCount() + ")");
			contentLabel.setFont(new Font("Dailog",Font.PLAIN,13));
			if(changeColor){
				contentLabel.setForeground(new Color(15,79,117));
			}
			item.add(contentLabel, BorderLayout.CENTER);

			final JLabel viewLable = new JLabel("查看");
			viewLable.setFont(new Font("Dailog",Font.PLAIN,13));
			viewLable.setVisible(false);
			item.add(viewLable, BorderLayout.EAST);
			viewLable.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					IntegrationManager.getWinInteraction().loginAppSys(
							mp.getAddress());
				}

				public void mouseEntered(MouseEvent e) {
					viewLable.setVisible(true);
					viewLable.setCursor(GraphicUtils.HAND_CURSOR);
					viewLable.setText("<html><u>" + "查看" + "</u></html>");
					viewLable.setForeground(Color.blue);
				}

				public void mouseExited(MouseEvent e) {
					viewLable.setVisible(false);
					viewLable.setText("查看");
					viewLable.setForeground(Color.black);
				}
			});
			item.addMouseListener(new MouseAdapter() {
				public void mouseEntered(MouseEvent e) {
					viewLable.setVisible(true);
				}

				public void mouseExited(MouseEvent e) {
					viewLable.setVisible(false);
				}
			});
			mainPanel.add(item);

			// mainPanel.add(new AppSysMessageItem("",bodyXML, "",
			// mp.getContentWithCount()));
			changeColor=!changeColor;
		}
//        AppSysRes.getImageIcon(AppSysRes.TOASTER_TITLE_TASK)
		showToaster("待办任务", mainPanel);
	}
	
	
	

}
